package com.cynthia.system.controller;


import com.cynthia.system.model.SysUser;
import com.cynthia.system.service.ISysUserService;
import com.cynthia.utils.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author cynthia
 * @since 2021-12-15
 */
@Controller
@RequestMapping("/sys-user")
public class SysUserController extends BaseController {
    /*//注入业务接口
    @Autowired
    private ISysUserService iSysUserService;*/

    /*-----查询-----用户列表-----*/
    @RequestMapping("/list")
    public String list(Model model){
        //内置的方法list
        List<SysUser> sysUserList_db=iSysUserService.queryUserList();
        model.addAttribute("sysUserList_db",sysUserList_db);
        System.out.println("===查询用户列表===list()===");
        return "/view/system/user/userList";
    }

    /*-----添加UI-----用户-----*/
    @RequestMapping("/addUI")
    public String addUI(Model model){
        System.out.println("===添加UI===addUI()===");
        return "/view/system/user/addUser";
    }

    /*-----添加-----用户-----*/
    @RequestMapping("/add")
    public String add(Model model,SysUser sysUser){
        //获取数据
        System.out.println(sysUser);
        //调用业务
        boolean b=iSysUserService.save(sysUser);
        //返回userList页面
        List<SysUser> sysUserList_db=iSysUserService.queryUserList();
        model.addAttribute("sysUserList_db",sysUserList_db);
        System.out.println("===添加用户===add()===");
        return "/view/system/user/userList";
    }

    /*-----删除-----通过userId删除用户-----*/
    @RequestMapping("/delete")
    public String delete(Model model,int userId){
        //获取数据
        System.out.println(userId);
        //调用业务
        boolean b=iSysUserService.removeById(userId);
        //返回userList页面
        List<SysUser> sysUserList_db=iSysUserService.queryUserList();
        model.addAttribute("sysUserList_db",sysUserList_db);
        System.out.println("===删除用户===delete()===");
        return "/view/system/user/userList";
    }

    /*-----修改UI-----用户-----*/
    @RequestMapping("/updateUI")
    public String updateUI(Model model,int userId){
        //接收数据
        System.out.println(userId);
        //调用业务
        //通过 userId用户ID查询对应的用户
        SysUser sysUser_db=iSysUserService.getById(userId);
        model.addAttribute("sysUser_db",sysUser_db);
        System.out.println("===修改UI===updateUI()===");
        //返回updateUser页面
        return "/view/system/user/updateUser";
    }

    /*-----修改-----用户-----*/
    @RequestMapping("/update")
    public String update(Model model,SysUser sysUser){
        //获取数据
        System.out.println(sysUser);
        //调用业务进行更新
        boolean b=iSysUserService.saveOrUpdate(sysUser);
        //返回userList页面
        List<SysUser> sysUserList_db=iSysUserService.queryUserList();
        model.addAttribute("sysUserList_db",sysUserList_db);
        System.out.println("===添加用户===update()===");
        return "/view/system/user/userList";
    }
}